<template>
    <div class='approval'>
      <el-card class="box-card">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
              <div class='searchItem'>
                <span>{{$t('dispatch.gcoll')}}</span>
                <el-select v-model='listQuery.groupIds' size='small' multiple>
                  <el-option
                  v-for='item in collGroup'
                  :key='item.groupBaseId'
                  :label='item.name'
                  :value='item.groupBaseId'>
                  </el-option>
                </el-select>
              </div>
              <div class='searchItem'>
                  <span>{{$t('RemindReport.collUser')}}</span>
                  <el-input v-model='listQuery.collectUser' :placeholder="$t('rule.collector')" size='small'></el-input>
              </div>
              <div class='searchItem searchItem-date'>
                <span>{{$t('ApprReport.choiceTime')}}</span>
                <el-date-picker
                size='small'
                v-model="listQuery.beginDate"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="$t('rule.beginDate')">
                </el-date-picker>
                <b>{{$t('approval.go')}}</b>
                <el-date-picker
                size='small'
                  v-model="listQuery.endDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.endDate')">
                </el-date-picker>
              </div>
              <div class='searchItem'>
                <span>{{$t('RushReport.loanType')}}</span>
                <el-select v-model='listQuery.type' size='small'>
                  <el-option
                  v-for='item in receRate'
                  :key='item.value'
                  :label='item.label'
                  :value='item.value'>
                  </el-option>
                </el-select>
              </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
                <button type='button' @click="handleDownload" :disabled="expDis" v-if="rushUser_btn_export">
                    {{$t('btn.export')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('processing.taskList')}}
          </div>
          <el-table
              :sum-text="$t('ApprReport.total')"
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              style='width: 100%'
              border
              show-summary
              :summary-method="getSummaries"
              size='mini'>
              <el-table-column
              header-align='center'
              align='center'
              prop='crtTime'
              width="140"
              :label="$t('approvalDetail.riskTime')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='collectGroup'
              width="140"
              :label="$t('dispatch.gcoll')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='collectUser'
              width="140"
              :label="$t('RemindReport.collUser')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='lastOrderCount'
              width="180"
              :label="$t('RushReport.lastOrderCount')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='newCollectingOrder'
              width="180"
              :label="$t('RushReport.newCollectingOrder')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='repayAmt'
              width="140"
              :label="$t('RushReport.amountAmt')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='realRepayAmt'
              width="140"
              :label="$t('RushReport.actualAmt')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='collectBack'
              width="140"
              :label="$t('RushReport.alreadyColl')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='orderCollectRate'
              width="140"
              :label="$t('RushReport.orderCollectRate')">
              </el-table-column>
              <el-table-column
              header-align='center'
              align='center'
              prop='repayRate'
              width="140"
              :label="$t('RushReport.receiveRate2')">
              </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[5, 10, 20, 30]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { pageUser, getCollectGroup2, pageExportUser } from 'api/reportForm/RushReport/index'
export default {
  data() {
    return {
      rushUser_btn_export: false,
      params: {
       "lastOrderCountSum": '', //上期留存案件数（件）
        "newCollectCountSum": '', //新增案件数（件）
		    "repayAmtSum": '', //应收金额（IDR）
        "realRepayAmtSum": '', //实收金额（IDR）
        "collectBackSum": '', //已催回件数（件）
        "orderCollectRateSum": '', //案件催回率（%）
        "repayRateAvgSum": '' //金额催回率（%）
      },
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 5,
        page: 1,
        beginDate: "",
        endDate: "",
        groupIds: "",//催收小组
        type: "2",
        collectUser: ""
      },
      tableData: [],
      collGroup: [
        {
          code: "",
          groupCrtTime: "",
          name: this.$t('rule.all'),
          groupBaseId: ""
        }
      ],
      receRate: [
        {
            value: '2',
            label: this.$t('rule.all')
        },
        {
            value: '1',
            label: this.$t('RushReport.artificial')
        },
        {
            value: '0',
            label: this.$t('RushReport.automatic')
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", "rushUserPage"])
  },
  created() {
    if (this.rushUserPage.has(this.$route.path)) {
      this.listQuery = this.rushUserPage.get(this.$route.path)
    }
    this.getTime();
    this.getList();
    //催收小组
    let group = {};
    getCollectGroup2(group)
    .then(response => {
      this.collGroup = this.collGroup.concat(response.data.collectList)
    })
    this.rushUser_btn_export = this.elements['rushUser:btn_export'];
  },
  methods: {
    getTime() {
      this.listQuery.beginDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    getSummaries(param) {
      const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          switch(index) {
            case 0:
              sums[index] = this.$t('ApprReport.total');
              break;
            case 3:
              sums[index] = this.params.lastOrderCountSum;
              break;
            case 4:
              sums[index] = this.params.newCollectCountSum;
              break;
            case 5:
              sums[index] = this.params.repayAmtSum;
              break;
            case 6:
              sums[index] = this.params.realRepayAmtSum;
              break;
            case 7:
              sums[index] = this.params.collectBackSum;
              break;
            case 8:
              sums[index] = this.params.orderCollectRateSum;
              break;
            case 9:
              sums[index] = this.params.repayRateAvgSum;
              break;
            default: 
              sums[index] = '--'
              break;
          }
        });
        return sums;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      pageExportUser(this.listQuery)
      .then(response => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = [ this.$t('approvalDetail.riskTime'), this.$t('dispatch.gcoll'), this.$t('RemindReport.collUser'), this.$t('RushReport.lastOrderCount'), this.$t('RushReport.newCollectingOrder'), this.$t('RushReport.amountAmt'), this.$t('RushReport.actualAmt'), this.$t('RushReport.alreadyColl'), this.$t('RushReport.orderCollectRate'), this.$t('RushReport.receiveRate2') ];
          const filterVal = [ 'crtTime', 'collectGroup', 'collectUser', 'lastOrderCount', 'newCollectingOrder', 'repayAmt', 'realRepayAmt', 'collectBack', 'orderCollectRate', 'repayRate' ];
          const list = response.page;
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, this.$t('RushReport.rushUser'))
          this.listLoading = false;
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveRushUserPage', { path: this.$route.path, listQuery: this.listQuery });
      pageUser(this.listQuery)
        .then(response => {
          response.page.records.forEach((item, index) => {
            item.index = index + (this.listQuery.page - 1) * this.listQuery.limit + 1
          })
          this.expDis = false;
          this.listLoading = false;
          this.tableData = response.page.records;
          this.total = Number(response.page.total);
          this.params = response.params
        })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.getTime();
      this.listQuery.endDate = "";
      this.listQuery.groupIds = [];
      this.listQuery.type = "2";
      this.listQuery.collectUser = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList()
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
</style>